import SysCircleApi from 'services/sysCircleApi';

export default {
  data() {
    return {
      circleZoneList: [],
      circleZoneMap: {}
    };
  },

  methods: {
    resetCircleZoneList(circleZoneList, isInit = false, isKeep = false) {
      if (isInit) {
        this.circleZoneMap = {};
      }

      let circleZoneData = [];
      let circleZoneMap = this.circleZoneMap;
      let ids = [];
      for (let i = 0; i < circleZoneList.length; i++) {
        const circleZone = circleZoneList[i];
        let spaceId = circleZone.spaceId;
        if (circleZoneMap[spaceId] !== undefined) {
          continue;
        }

        circleZoneMap[spaceId] = circleZone;
        circleZoneData.push(circleZone);
        ids.push(spaceId);
      }

      if (isInit) {
        this.circleZoneList = circleZoneData;
      } else {
        this.circleZoneList.push(...circleZoneData);
      }

      if (isKeep) {
        return window.Promise.resolve(circleZoneData);
      }

      return SysCircleApi.readCircleZone(ids).then(() => {
        return circleZoneData;
      });
    },

    getCircleZoneObjById(spaceId) {
      let circleZoneMap = this.circleZoneMap;
      let circle = circleZoneMap[spaceId];
      if (circle === undefined) {
        return null;
      }
      return circle;
    }
  }
}
